package com.skydroid.demo.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.skydroid.demo.R
import com.skydroid.demo.base.BaseSocketSerialActivity
import com.skydroid.demo.common.Constants
import com.skydroid.demo.databinding.ActivityChannelCheckBinding
import com.skydroid.demo.utils.LogUtils
import com.skydroid.demo.utils.String2ByteArrayUtils
import com.zhouyou.view.seekbar.SignSeekBar


/**
 * 舵量查看
 */
class ChannelCheckActivity: BaseSocketSerialActivity() {

    private var mBind: ActivityChannelCheckBinding? = null
    private var mSeekBarList = ArrayList<SignSeekBar?>()
    private var mTVList = ArrayList<TextView?>()
    init {
        bindCode = "uart0app"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_channel_check)

        initView()
    }

    companion object{
        fun start(context: Context){
            context.startActivity(
                Intent(context,
                    ChannelCheckActivity::class.java)
            )
        }
    }

    private fun initView(){
        mSeekBarList?.add(mBind?.item1?.searchBar)
        mSeekBarList?.add(mBind?.item2?.searchBar)
        mSeekBarList?.add(mBind?.item3?.searchBar)
        mSeekBarList?.add(mBind?.item4?.searchBar)
        mSeekBarList?.add(mBind?.item5?.searchBar)
        mSeekBarList?.add(mBind?.item6?.searchBar)
        mSeekBarList?.add(mBind?.item7?.searchBar)
        mSeekBarList?.add(mBind?.item8?.searchBar)
        mSeekBarList?.add(mBind?.item9?.searchBar)
        mSeekBarList?.add(mBind?.item10?.searchBar)
        mSeekBarList?.add(mBind?.item11?.searchBar)
        mSeekBarList?.add(mBind?.item12?.searchBar)
        mSeekBarList?.add(mBind?.item13?.searchBar)
        mSeekBarList?.add(mBind?.item14?.searchBar)
        mSeekBarList?.add(mBind?.item15?.searchBar)
        mSeekBarList?.add(mBind?.item16?.searchBar)


        mTVList?.add(mBind?.item1?.tv)
        mTVList?.add(mBind?.item2?.tv)
        mTVList?.add(mBind?.item3?.tv)
        mTVList?.add(mBind?.item4?.tv)
        mTVList?.add(mBind?.item5?.tv)
        mTVList?.add(mBind?.item6?.tv)
        mTVList?.add(mBind?.item7?.tv)
        mTVList?.add(mBind?.item8?.tv)
        mTVList?.add(mBind?.item9?.tv)
        mTVList?.add(mBind?.item10?.tv)
        mTVList?.add(mBind?.item11?.tv)
        mTVList?.add(mBind?.item12?.tv)
        mTVList?.add(mBind?.item13?.tv)
        mTVList?.add(mBind?.item14?.tv)
        mTVList?.add(mBind?.item15?.tv)
        mTVList?.add(mBind?.item16?.tv)



        for (v in mSeekBarList){
            v?.isClickable = false
            v?.isEnabled = false
            v?.isFocusable = false
        }
    }

    override fun read() {
        Thread(Runnable {
            try {
                while (true) {
                    val inputStream = localSocket?.inputStream
                    val len = inputStream?.read(mReadBuffer.array())?:0
                    if (len > 0) {
                        val data = ByteArray(len)
                        mReadBuffer[data, 0, len]
                        mainHanlder.post(
                            Runnable {
                                //数据处理 receive(data)
                                receive(data)
                            })
                        mReadBuffer.clear()
                    }
                }
            } catch (e:Exception) {
                e.printStackTrace()
            }
        }).start()
    }

    override fun receive(datas: ByteArray?) {

        datas?.let {
            var hexStr = String2ByteArrayUtils.bytes2Hex(datas)
            LogUtils.test("读到的数据$hexStr")
            if(TextUtils.isEmpty(hexStr)){
                return
            }

            if(!hexStr!!.contains(Constants.SKYDROID_PACKAGE_HEADER)){
                return
            }

            var funtionIndex = hexStr.indexOf(Constants.SKYDROID_PACKAGE_HEADER) + (Constants.SKYDROID_PACKAGE_HEADER.length / 2)

            when(String2ByteArrayUtils.byte2Hex(datas[funtionIndex])){
                Constants.B1->{
                    var length = datas[funtionIndex + 1]
                    if(datas.size < (funtionIndex + 1 + length + 1 )){
                        return
                    }

                    var startIndex = funtionIndex + 1 + 1
                    var sb = StringBuilder()
                    for (index in 0 until length step 2){
                        var h = datas[ startIndex + index]
                        var l = datas[ startIndex + index + 1]
                        sb.append("CH${index +1}：")
                            .append( setSeekBar(index/2 , h , l))
                            .append("  ")

                    }
                    LogUtils.test("ppm sum:${sb.toString()}")
                }
            }
        }


    }

    private fun setSeekBar(index :Int, h :Byte, l :Byte) :Int{
        //ppm=sbus*5/8+874
        var bytes = byteArrayOf(0,0,h,l)
        var sbus =String2ByteArrayUtils.bytes2int(bytes)
        var ppm = ((sbus * 5) / 8) + 874
//        LogUtils.test("h:${h}   " + "l:${l}   " + "Sbus:$sbus")


        mSeekBarList?.get(index)?.setProgress(ppm.toFloat())
        mTVList?.get(index)?.text = "CH${index +1}"
//        LogUtils.test("progress:${mSeekBarList?.get(index)?.progress}")
        return ppm
    }

}